#region File Description
//-----------------------------------------------------------------------------
// OptionsMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;
using AwesomeTanks.Sources.Media;
#endregion

namespace AwesomeTanks.Sources.Screens
{
    /// <summary>
    /// The options screen is brought up over the top of the main menu
    /// screen, and gives the user a chance to configure the game
    /// in various hopefully useful ways.
    /// </summary>
    public class OptionsMenuScreen : MenuScreen
    {
        #region Fields

        public const int OPTION_MENU_Y_OFFSET = 200;
        public const int OPTION_MENU_ENTRIES_MARGIN_Y = 5;

        private MenuEntry GameTypeMenuEntry;
        private MenuEntry TimeMenuEntry;
        private MenuEntry VibrationMenuEntry;
        private MenuEntry LifeMenuEntry;
        private MenuEntry MapMenuEntry;
        private MenuEntry MuteMenuEntry;
        private MenuEntry MuteEffectsMenuEntry;
        private MenuEntry FriendlyFireMenuEntry;
        private MenuEntry BouncingShootMenuEntry;

        public enum GameType
        {
            OneVsOne,
            TwoVsTwo,
            FreeForAllAt3,
            FreeForAllAt4,
        }

        public static GameType currentGameType = GameType.OneVsOne;

        public static string[] times = { "1:30", "2:00", "4:00","5:00","10:00", "Infinite" };
        public static int currentTime = 0;

        public static bool Vibration = true;

        public static int[] lifes = { 0, 1, 2, 3, 4, 5, 10 };
        public static int CurrentLife = 3;

        public static string[] maps = { "Empty", "Cross", "Moria", "Jarvan", "Koh-Lanta","Random" };
        public static int CurrentMap = 1;

        public static bool Mute = false;
        public static bool MuteEffect = false;

        public static bool FriendlyFire = false;

        public static bool BoucingShoot = false;


        /// <summary>
        /// Usefull to limit the options if the game is already started.
        /// </summary>
        private bool inGameOption;

        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public OptionsMenuScreen(bool gameStarted)
            : base("Options")
        {
            this.inGameOption = gameStarted;
            this.OffsetY = OPTION_MENU_Y_OFFSET;
            this.MenuEntriesMarginY = OPTION_MENU_ENTRIES_MARGIN_Y;

            // Create our menu entries.
            if (!gameStarted)
            {
                GameTypeMenuEntry = new MenuEntry(string.Empty);
                TimeMenuEntry = new MenuEntry(string.Empty);
                LifeMenuEntry = new MenuEntry(string.Empty);
                MapMenuEntry = new MenuEntry(string.Empty);
                FriendlyFireMenuEntry = new MenuEntry(string.Empty);
                BouncingShootMenuEntry = new MenuEntry(string.Empty);
            }
            
            VibrationMenuEntry = new MenuEntry(string.Empty);
            MuteMenuEntry = new MenuEntry(string.Empty);
            MuteEffectsMenuEntry = new MenuEntry(string.Empty);


            SetMenuEntryText();

            MenuEntry back = new MenuEntry("Back");

            // Hook up menu event handlers.
            if (!gameStarted)
            {
                GameTypeMenuEntry.Selected += GameTypeMenuEntrySelected;
                TimeMenuEntry.Selected += TimeMenuEntrySelected;
                LifeMenuEntry.Selected += LifeMenuEntrySelected;
                MapMenuEntry.Selected += MapMenuEntrySelected;
                FriendlyFireMenuEntry.Selected += FriendlyFireMenuEntrySelected;
                BouncingShootMenuEntry.Selected += BoucingShootMenuEntrySelected;
            }
            
            VibrationMenuEntry.Selected += VibrationMenuEntrySelected;            
            MuteMenuEntry.Selected += MuteMenuEntrySelected;
            MuteEffectsMenuEntry.Selected += MuteEffectsMenuEntrySelected;
            back.Selected += OnCancel;

            // Add entries to the menu.
            if (!gameStarted)
            {
                MenuEntries.Add(GameTypeMenuEntry);
                MenuEntries.Add(FriendlyFireMenuEntry);                
                MenuEntries.Add(LifeMenuEntry);
                MenuEntries.Add(MapMenuEntry);
                MenuEntries.Add(TimeMenuEntry);
                MenuEntries.Add(BouncingShootMenuEntry);
            }
            MenuEntries.Add(MuteMenuEntry);
            MenuEntries.Add(MuteEffectsMenuEntry);
            MenuEntries.Add(VibrationMenuEntry);
            MenuEntries.Add(back);
        }


        /// <summary>
        /// Fills in the latest values for the options screen menu text.
        /// </summary>
        void SetMenuEntryText()
        {
            if (!inGameOption)
            {
                GameTypeMenuEntry.Text = "Game Type: " + currentGameType;
                TimeMenuEntry.Text = "Time: " + times[currentTime];
                LifeMenuEntry.Text = "Lifes: " + lifes[CurrentLife];
                MapMenuEntry.Text = "Map: " + maps[CurrentMap];
                FriendlyFireMenuEntry.Text = "Friendly Fire: " + (FriendlyFire ? "on" : "off");
                BouncingShootMenuEntry.Text = "Bouncing shoot: " + (BoucingShoot ? "on" : "off");
            }
            VibrationMenuEntry.Text = "Vibration: " + (Vibration ? "on" : "off");
            MuteMenuEntry.Text = "Mute: " + (Mute ? "on" : "off");
            MuteEffectsMenuEntry.Text = "Mute Effects: " + (MuteEffect ? "on" : "off");
        }


        #endregion

        #region Handle Input

        /// <summary>
        /// Event handler for when the Game Type menu entry is selected.
        /// </summary>
        void GameTypeMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentGameType++;

            if (currentGameType > GameType.FreeForAllAt4)
                currentGameType = 0;

            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Time menu entry is selected.
        /// </summary>
        void TimeMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            currentTime = (currentTime + 1) % times.Length;

            SetMenuEntryText();

            if (times[currentTime].Equals("Infinite"))
            {
                Sound.ItsOver9000.Play(1f, 0f, 0f);
            }
        }


        /// <summary>
        /// Event handler for when the Vibration menu entry is selected.
        /// </summary>
        void VibrationMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            Vibration = !Vibration;

            SetMenuEntryText();
        }


        /// <summary>
        /// Event handler for when the Life menu entry is selected.
        /// </summary>
        void LifeMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            CurrentLife = (CurrentLife + 1) % lifes.Length;

            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Map menu entry is selected.
        /// </summary>
        void MapMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            CurrentMap = (CurrentMap + 1) % maps.Length;

            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the Mute menu entry is selected.
        /// </summary>
        void MuteMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            Mute = !Mute;

            if (Mute)
            {
                MediaPlayer.Pause();
            }
            else
            {
                MediaPlayer.Resume();
            }

            SetMenuEntryText();
        }

        /// <summary>
        /// Event handler for when the MuteEffects menu entry is selected.
        /// </summary>
        void MuteEffectsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            MuteEffect = !MuteEffect;

            SetMenuEntryText();
        }

        /// <summary>
        /// Mutes the effects menu entry selected.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="AwesomeTanks.Sources.Screens.PlayerIndexEventArgs" /> instance containing the event data.</param>
        void FriendlyFireMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            FriendlyFire = !FriendlyFire;

            SetMenuEntryText();
        }

        /// <summary>
        /// Boucings the shoot menu entry selected.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="PlayerIndexEventArgs"/> instance containing the event data.</param>
        void BoucingShootMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        {
            BoucingShoot = !BoucingShoot;

            SetMenuEntryText();
        }
        #endregion
    }
}
